part of leancloud_rest_api;

class LraQuery {
  Map<String, dynamic>? where = {};

  int limit = 100;
  int? skip = 0;
  int? count;

  List<String>? orderByList;
  Set<String>? includes;
  Set<String>? selectedKeys;

  LraQuery(
      {this.where,
      this.orderByList,
      this.limit = 100,
      this.skip,
      this.selectedKeys,
      this.count,
      this.includes});

  Map<String, dynamic> buildParams() {
    Map<String, dynamic> result = {'skip': skip, 'limit': limit};
    if (where != null) {
      result['where'] = _buildWhere();
    }
    String? orders = _buildOrder();
    if (orders != null) {
      result['order'] = orders;
    }
    String? includes = _buildIncludes();
    if (includes != null) {
      result['include'] = includes;
    }
    String? selectedKeys = _buildSelectedKeys();
    if (selectedKeys != null) {
      result['keys'] = selectedKeys;
    }
    return result;
  }

  String? _buildWhere() {
    if (where != null) {
      return jsonEncode(_LCEncoder.encode(where));
    }
    return null;
  }

  String? _buildOrder() {
    if (orderByList != null && orderByList!.isNotEmpty) {
      return orderByList!.join(',');
    }
    return null;
  }

  String? _buildIncludes() {
    if (includes != null && includes!.isNotEmpty) {
      return includes!.join(',');
    }
    return null;
  }

  String? _buildSelectedKeys() {
    if (selectedKeys != null && selectedKeys!.isNotEmpty) {
      return selectedKeys!.join(',');
    }
    return null;
  }
}
